package cn.nju.zyy.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import cn.nju.zyy.dao.UserDAO;
import cn.nju.zyy.domain.User;
import cn.nju.zyy.service.UserService;

public class UserController extends MultiActionController {

	private UserDAO userDAO;
	private UserService userService;

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public ModelAndView add(HttpServletRequest request,
			HttpServletResponse response, User user) throws Exception {
		userDAO.saveUser(user);
		return new ModelAndView("redirect:list.html");
	}

	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelMap model = new ModelMap();
		model.addAttribute("countryList", userService.getAllCountries());
		model.addAttribute("communityList", userService.getAllCommunities());
		model.addAttribute("userList", userDAO.listUser());
		model.addAttribute("user", new User());
		return new ModelAndView("userForm", model);
	}

	/**
	 * In this method you can set all the default values that should be loaded
	 * when the form is displayed to the user. This method will be called
	 * automatically.
	 */
	// @SuppressWarnings({ "rawtypes", "unchecked" })
	// @Override
	// protected Map referenceData(HttpServletRequest request) throws Exception
	// {
	// Map referenceData = new HashMap();
	// referenceData.put("countryList", userService.getAllCountries());
	// referenceData.put("communityList", userService.getAllCommunities());
	// return referenceData;
	// }
	//
	// @Override
	// protected ModelAndView onSubmit(Object command) throws Exception {
	// User user = (User) command;
	// userService.add(user);
	// return new ModelAndView("userSuccess", "user", user);
	// }

}
